#!/usr/bin/perl
#Common function for editing the atalk config files

#
#    Netatalk Webmin Module
#    Copyright (C) 2000 by Matthew Keller <kellermg@potsdam.edu>
#    Some code (C) 2000 by Sven Mosimann/EcoLogic <sven.mosimann@ecologic.ch>
#    Contributions from:
#       Sven Mosimann <sven.mosimann@ecologic.ch>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

# This module is almost all Sven's. I'm merging it into "the"
#  Netatalk webmin lib module. I've had to update it to the "new"
#  config file format, as it was using the older format.
# I've held off merging some of this, but it is on my TODO. Before this
#  module comes out of "BETA" I want ONE function module, not 2.

do '../web-lib.pl';

&init_config();

#ez_chg_line(\%afshare)
#return $str_line
sub ez_chg_line()
{
	my ($afshare, @usr, @gp, $i, $k, $str_line, $str_group, $str_user);	

	$afshare = @_[0];
	$str_line = "";
#dir
	$str_line = $afshare->{'path'};
#sharename
	$str_line .= " \"";
	$str_line .= $afshare->{'share'};
	$str_line .= "\"";

#codepage maccode asia
	$str_line .= " codepage:maccode.asia";	

#allow
	if($afshare->{'allowusers'} || $afshare->{'allowgroups'})
	{
		$str_line .= " allow:";
		@usr = split(/\s+/, $afshare->{'allowusers'});
		@gp = split(/\s+/, $afshare->{'allowgroups'});

		$str_user = join(',', @usr);
		$str_group = join(',@', @gp);

		$str_line .= $str_user if ($str_user);
		$str_line .= ',' if ($str_user && $str_group);
		$str_line .= '@' . $str_group if ($str_group);
	}
#deny
	if($afshare->{'denyusers'} || $afshare->{'denygroups'})
	{
		$str_line .= " deny:";
		@usr = split(/\s+/, $afshare->{'denyusers'});
		@gp = split(/\s+/, $afshare->{'denygroups'});

		$str_user = join(',', @usr);
		$str_group = join(',@', @gp);

		$str_line .= $str_user if ($str_user);
		$str_line .= ',' if ($str_user && $str_group);
		$str_line .= '@' . $str_group if ($str_group);
	}

#rolist
	if($afshare->{'rousers'} || $afshare->{'rogroups'})
	{

		$str_line .= " rolist:";
		@usr = split(/\s+/, $afshare->{'rousers'});
		@gp = split(/\s+/, $afshare->{'rogroups'});

		$str_user = join(',', @usr);
		$str_group = join(',@', @gp);

		$str_line .= $str_user if ($str_user);
		$str_line .= ',' if ($str_user && $str_group);
		$str_line .= '@' . $str_group if ($str_group);
	}

#rwlist
	if($afshare->{'rwusers'} || $afshare->{'rwgroups'})
	{
		$str_line .= " rwlist:";
		@usr = split(/\s+/, $afshare->{'rwusers'});
		@gp = split(/\s+/, $afshare->{'rwgroups'});

		$str_user = join(',', @usr);
		$str_group = join(',@', @gp);

		$str_line .= $str_user if ($str_user);
		$str_line .= ',' if ($str_user && $str_group);
		$str_line .= '@' . $str_group if ($str_group);
	}

#password
	if($afshare->{'password'})
	{
		$str_line .= " password:";
		$str_line .= $afshare->{'password'};
	}

#options
	if($afshare->{'options'})
	{
		$str_line .= " options:";
		$str_line .= $afshare->{'options'};
	}

	return $str_line;
}

sub ez_del_afp($sharepath)
{
	my (@file, $i, $path, @line, $lref);

	$path = @_[0];

	unless($path)
	{
		return $text{'msg_err_delafp_nopath'};
	}

	$lref = &read_file_lines($config{'applevolumedefault_c'});
	for($i = @$lref - 1; $i >= 0; $i--)
	{
		@line = split(/\s+/, $lref->[$i]);
		if($line[0] eq $path)
		{
			splice @$lref, $i, 1;
		}
	}

	&flush_file_lines();

	return "";
}

#ez_get_afp(\%afshare, $sharepath)
sub ez_get_afp()
{
	my (@file, $i, $k, $afshare, @line, $str, $sharepath);

	$afshare = @_[0];
	$sharepath = @_[1];

	open(AFPVOL, $config{'applevolumedefault_c'});
	@file = <AFPVOL>;
	close(AFPVOL);

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);
		@line = split(/\s+/ ,$file[$i]);

		if($line[0] eq $sharepath)
		{
			$afshare->{'path'} = $line[0];
			$str = $file[$i];
			$str =~ /"(.*)"/;
			$afshare->{'share'} = $1;

			#delete path and sharename
			$str =~ s/^.*".*"//;
			$str =~ s/^\s+//;

			@line = split(/\s+/, $str);
			foreach $k (@line)
			{
				if($k =~ /allow:(.*)$/)
				{
					$afshare->{'allow'} = $1;
					next;
				}
				if($k =~ /deny:(.*)$/)
				{
					$afshare->{'deny'} = $1;
					next;
				}
				if($k =~ /rolist:(.*)$/)
				{
					$afshare->{'rolist'} = $1;
					next;
				}
				if($k =~ /rwlist:(.*)$/)
				{
					$afshare->{'rwlist'} = $1;
					next;
				}
				if($k =~ /options:(.*)$/)
				{
					$afshare->{'options'} = $1;
					next;
				}
				if($k =~ /password:(.*)$/)
				{
					$afshare->{'password'} = $1;
					next;
				}
			}
			last;
		}
	}
}

#it not in netatalk.wbm is for ezstornas
#ez_save_afp($shareline, $path) is also like writeFile
sub ez_save_afp()
{
	my (@file, $i, $path, @line, $shareline, $lref);

	$shareline = @_[0];
	$path = @_[1];

	unless($path)
	{
		return $text{'msg_err_delafp_nopath'};
	}

	$lref = &read_file_lines($config{'applevolumedefault_c'});
	for($i = @$lref - 1; $i >= 0; $i--)
	{
		@line = split(/\s+/, $lref->[$i]);
		if($line[0] eq $path)
		{
			splice @$lref, $i, 1;
		}
	}

	push @$lref, $shareline;
	&flush_file_lines();

	return "";
}

#ez_afp_chg_servline(\%servconf)
#return $servline
sub ez_afp_chg_servline()
{
	my ($ptconf, $servline);

	$ptconf = @_[0];

	$servline = "";
	
	$servline .= " " . $ptconf->{'uamlist'} if ($ptconf->{'uamlist'});
	$servline .= " " . $ptconf->{'timeout'} if ($ptconf->{'timeout'});

	$servline .= " -nosavepassword -nosetpassword";

	return $servline;
}

#ez_save_afpserver($servline)
sub ez_save_afpserver()
{
	my ($lerf, $i, $n, $oldhostname, $servline, @line, $str, $hostname);

	$servline = @_[0];

	$hostname = `hostname`;

	chomp($hostname);
	$hostname =~ s/^\s+//;

	$lref = &read_file_lines($config{'afpd_c'});

#	print @$lref;

	for($i = @$lref - 1; $i >= 0; $i--)
	{
		$lref->[$i] =~ s/^[^#]*#/#/;
		if($lref->[$i] !~ /#/)
		{
			splice @$lref, $i, 1;
		}
	}

	push @$lref, "-" . " " . $servline;
	push @$lref, '"' . $hostname . '"' . " -ddp " . $servline;
	&flush_file_lines();
}


#ez_save_maxsessions($maxsessions)
sub ez_save_maxsessions()
{
	my ($maxsessions, $lref, $i, @line);

	$maxsessions = @_[0];

	unless($maxsessions)
	{
		$maxsessions = 20;
		#20 sessions
	}

	$lref = &read_file_lines($config{'netatalk_c'});

	for($i = @$lref - 1; $i >= 0; $i--)
	{
		$lref->[$i] =~ s/^[^#]*#/#/;
		if($lref->[$i] !~ /#/)
		{
			@line = split('=', $lref->[$i]);
			if($line[0] eq 'AFPD_MAX_CLIENTS')
			{
				$lref->[$i] = 'AFPD_MAX_CLIENTS=' . $maxsessions;
				goto WRITEFILE;
			}
		}
	}

	push @$lref, 'AFPD_MAX_CLIENTS=' . $maxsessions;

	WRITEFILE:

	&flush_file_lines();
}

#ez_save_afpserver(\%afpserver)
sub ez_get_afpserver()
{
	my (@file, $i, $str, $ptservconf, @line, $k);

	$ptservconf = @_[0];

	open(AFPCONF, $config{'afpd_c'});
	@file = <AFPCONF>;
	close(AFPCONF);

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);

		$file[$i] =~ s/^\s+//;
		@line = split(/\s+/, $file[$i]);
		if($line[0] eq '-')
		{
			$str = $file[$i];
			last;
		}
	}

	@line = split('-', $str);
	for($i = 0; $i < @line; $i++)
	{
		if($line[$i] =~ /uamlist/)
		{
			$ptservconf->{'uamlist'} = "-" . $line[$i];
			next;
		}

		if($line[$i] =~ /timeout/)
		{
			$ptservconf->{'timeout'} = "-" . $line[$i];
			next;
		}
	}
}

#ez_get_maxsessions()
sub ez_get_maxsessions()
{
	my (@file, $i);

	open(NATALKCONF, $config{'netatalk_c'});
	@file = <NATALKCONF>;
	close(NATALKCONF);

	for($i = 0; $i < @file; $i++)
	{
		$file[$i] =~ s/#.*$//;
		chomp($file[$i]);

		if($file[$i] =~ /AFPD_MAX_CLIENTS=(.*)/)
		{
			return $1;
		}
	}
}

1;
